package com.niknik.woody.utils.wunit.log;

import static com.niknik.woody.utils.WUtils.firstNotNull;

final class InterceptableLogger
    implements WLogger {

  private static volatile WLogger interceptor = null;
  private static volatile WLogger delegate;

  InterceptableLogger(WLogger logger) {
    delegate = logger;
  }

  private WLogger getActualLogger() {
    return firstNotNull(interceptor, delegate);
  }

  @Override
  public void methodCall(String msg, String... params) {
    getActualLogger().methodCall(msg, params);
  }

  @Override
  public void exThrow(Throwable ex) {
    getActualLogger().exThrow(ex);
  }

  @Override
  public void exCatch(Throwable ex) {
    getActualLogger().exCatch(ex);
  }

  @Override
  public void fatalExCatch(Throwable ex, String msg, String... params) {
    getActualLogger().fatalExCatch(ex, msg, params);
  }

  static void interceptWith(WLogger interceptor) {
    InterceptableLogger.interceptor = interceptor;
  }

  static void removeInterceptor() {
    interceptor = null;
  }

  static boolean isIntercepted() {
    return interceptor != null;
  }
}